# Maintainer: Lukas Fleischer <lfleischer@archlinux.org>
# Contributor: Roman Kyrylych <roman@archlinux.org>
# Contributor: Sven Kauber <celeon@gmail.com>
# Contributor: tardo <tardo@nagi-fanboi.net>
# Contributor: Daniel J Griffiths <ghost1227@archlinux.us>

pkgname=partimage
pkgver=0.6.9
pkgrel=14
pkgdesc='Partition Image saves partitions in many formats to an image file.'
arch=('loong64' 'x86_64')
url='https://www.partimage.org/'
license=('GPL')
depends=('libnewt' 'bzip2' 'openssl')
backup=('etc/partimaged/partimagedusers')
source=("https://downloads.sourceforge.net/${pkgname}/${pkgname}-${pkgver}.tar.bz2"
        'partimage-0.6.9-zlib-1.2.6.patch'
        'use-SSLv3-by-default.patch'
        'partimage-build-fix.patch'
        'partimaged-gencrt'
        'partimaged.service'
        'partimage.sysusers')
md5sums=('1bc046fd915c5debbafc85729464e513'
         '202c4334766041d98c5f67c9d62c3063'
         '77c33f779186cd25e7c6543348ca5009'
         'c84a2b59ecf9e554806b11ed1735ef72'
         '2316b116227b07efb48266e660ca1ba1'
         '5f8dae511cdfbf097409a45e9642a372'
         'b0773e9d666e06301c2dcbec80ff25ac')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  patch -Np1 -i ../partimage-0.6.9-zlib-1.2.6.patch
  patch -Np1 -i ../use-SSLv3-by-default.patch
  patch -p1 -i ../partimage-build-fix.patch # Fix build with recent glibc

  sed -e 's/CRYPTO_lock/X509_new/g' -i configure
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --sbindir=/usr/bin \
    --enable-pam

  make
  make pamfile
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make DESTDIR="${pkgdir}" install

  install -Dm0644 partimaged.pam "${pkgdir}/etc/pam.d/partimaged"
  install -Dm0755 "${srcdir}/partimaged-gencrt" "${pkgdir}/usr/bin/partimaged-gencrt"

  chmod 644 "${pkgdir}/etc/partimaged/partimagedusers"
  chown 110:110 "${pkgdir}/etc/partimaged/partimagedusers"

  install -Dm0644 ../partimaged.service "${pkgdir}/usr/lib/systemd/system/partimaged.service"
  install -Dm0644 ../partimage.sysusers "${pkgdir}/usr/lib/sysusers.d/partimage.conf"
}
